package com.zfei.gfb.bean;

import com.zfei.gfb.api.domain.OrderType;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.function.LongSupplier;

/**
 * @author fgh
 */
public class Page implements Serializable {

    @ApiModelProperty(value = "开始时间")
    private Long minTime;
    @ApiModelProperty(value = "结束时间")
    private Long maxTime;

    private Integer offset;
    @ApiModelProperty(value = "每页大小", required = true)
    private Integer limit;
    @ApiModelProperty(value = "当前页", required = true)
    private Integer pageNo;

    /**
     * 优质优先
     */

    public Page() {
    }

    public Page(Long minTime, Long maxTime, Integer limit, Integer pageNo) {
        this.minTime = minTime;
        this.maxTime = maxTime;
        this.limit = limit;
        this.pageNo = pageNo;
    }

    /**
     * 获取所有数据的分页
     */
    public static Page buildDefault() {
        Page page = new Page();
        page.build(System::currentTimeMillis);
        page.setLimit(Integer.MAX_VALUE);
        return page;
    }

    /**
     * 获取所有数据的分页
     */
    public static Page buildDefault(Integer count) {
        Page page = new Page();
        page.build(System::currentTimeMillis);
        page.setLimit(count);
        return page;
    }

    public Long getMinTime() {
        return minTime;
    }

    public void setMinTime(Long minTime) {
        this.minTime = minTime;
    }

    public Long getMaxTime() {
        return maxTime;
    }

    public void setMaxTime(Long maxTime) {
        this.maxTime = maxTime;
    }

    public Integer getLimit() {
        return limit;
    }

    public Page setLimit(Integer limit) {
        this.limit = limit;
        return this;
    }

    public Page build(LongSupplier supplier) {
        if (minTime == null || minTime < 0) {
            minTime = 0L;
        }
        if (supplier != null && (maxTime == null || maxTime <= 0)) {
            maxTime = supplier.getAsLong();
        }
        if (limit == null || limit <= 0) {
            limit = 5;
        }
        if (offset == null || offset < 0) {
            offset = 0;
        }
        if (pageNo != null && pageNo > 0) {
            offset = (pageNo - 1) * limit;
        }

        //query参数如传""，转化为null
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object val = field.get(this);
                if (val instanceof String && StringUtils.isBlank((String) val)) {
                    field.set(this, null);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return this;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getOffset() {
        return offset;
    }
}
